<?php

class DetailController extends Controller
{
      public $layout = 'applayout';
              private $_news;
         public function actionView($id)
        {
                $news = News::model()->findByPK($id);
                $this->_news = $news;
                $comment_form = $this->newComment($news);
                $category = Category::model( )->findAllByPk($news->category_id);
                $categories = Category::model( )->findAll();
               $ads = Ads::model()->findall(); // tao ra dataProvider moi
                $login_form=new LoginForm;
                $comments = Comment::model()->findAllByAttributes(array('news_id' => $id));
		$this->render('view', array(
	                'comment_form' => $comment_form,
	                'comments'=>$comments,
	                'news' => $news,
                        'category' => $category,
                        'categories' => $categories,
                        'login_form' => $login_form,
                        'ads' => $ads,
                        ''
                ));
        }
        
        
                private function newComment($news) {
        	$comment = new Comment();
        	
        	if(isset($_POST['Comment'])) {
				$comment->comment_author = $_POST['Comment']['comment_author'];
				$comment->comment_title = $_POST['Comment']['comment_title'];
				$comment->comment_content = $_POST['Comment']['comment_content'];
				$comment->comment_email = $_POST['Comment']['comment_email'];
        		$comment->news_id = $news->news_id;
        
        		$comment->save();
        	}
        		
        	return $comment;
        }
	// Uncomment the following methods and override them if needed
	/*
	public function filters()
	{
		// return the filter configuration for this controller, e.g.:
		return array(
			'inlineFilterName',
			array(
				'class'=>'path.to.FilterClass',
				'propertyName'=>'propertyValue',
			),
		);
	}

	public function actions()
	{
		// return external action classes, e.g.:
		return array(
			'action1'=>'path.to.ActionClass',
			'action2'=>array(
				'class'=>'path.to.AnotherActionClass',
				'propertyName'=>'propertyValue',
			),
		);
	}
	*/
}